#ifndef ACCOUNT_MANAGER_H
#define ACCOUNT_MANAGER_H

// Qt includes
#include <QObject>
#include <QVariant>
#include <QTimer>


namespace Network {
class HttpRequest;
}


namespace Account {
class Member;
class User;
class Friend;
class GetFriendRequest;
class ProfileRequest;
class OnlineStatusRequest;
class ProfileResponse;
class OnlineStatusEvent;
class Event;
class Parser;
class LoginResponse;
class LoginRequest;
class RecentContactEvent;
class RecentContactRequest;

class Manager : public QObject
{
    Q_OBJECT
public:
    typedef QList<Friend *> Friends;
    typedef QMap<int, Friend *> Finder;
    typedef QList<Parser *> Parsers;
    typedef QMap<int, int> IndexFinder;

public:
    explicit Manager(QObject *parent = 0);

public:
    static Manager *instance(QObject *parent = 0);

public:
    /**
     * @brief init call when login ok
     * @param loginResponse
     */
    void init(const LoginResponse& loginResponse);

    const Member *find(int userId) const;

    Member *find(int id);

    Friend *findFriend(int id);

    int getFriendIndex(int memberId) const;
    int friendCount() const;

public:
    inline const Friends &friends() const {
        return m_friends;
    }

    inline Friends &friends() {
        return m_friends;
    }

    inline const User *user() const {
        return m_user;
    }

    inline User *user() {
        return m_user;
    }

    void unregisterParser(Account::Parser *parser);
    void clearData();

signals:
    void onlineStatusChanged(const Friend *aFriend);
    void gotProfile();
    void gotStatus();
    void profileGotten(const Member *member);
    void avatarReady(const Member *member);
    void messageStatusGotten(const Member *member);
    void friendReceived();
    void friendsReceived(int numberOfFriends);
    void dataCleared();
    void recentContactTimeUpdated();

private slots:
    void onAvatarReady(const Member *member);
    void onSessionKeyReady();
    void onLogouted();

private:
    void setConnection() const;
    void deleteUser();
    void deleteBuddies();
    void deleteParsers();
    void clearIndexFinder();
    void clearBuddyFinder();


private:
    static Manager *m_instance;

private:
    User *m_user;
    Friends m_friends;
    Finder m_finder;

    int m_getFriendRequestPage;

    QTimer *m_updateOnlineStatusTimer;
    OnlineStatusRequest *m_onlineStatusRequest;
    RecentContactRequest *m_recentContactRequest;

    Parsers m_parsers;
    IndexFinder m_indexFinder;
    bool m_enable; // logout m_enable = false; else m_enable = true;
};

} // namespace Account
#endif // ACCOUNT_MANAGER_H
